#coding:utf-8
#this is the code module to play background music and sound effect
#version:0.201
#without error check

import pygame
from pygame.mixer import music
import os

import define

def stop():
    pygame.mixer.stop()
def pause():
    pygame.mixer.pause()
    
class BGM_FX(object):
    def __init__(self):
        self.name = "BGMFX"
        self.filename = ""
    def play(self,filename,loops=-1,start=0.0):
        if not self.filename:
            self.stop()
        self.filename = filename
        try:
            music.load(filename)
        except:
            print "bad filename or i/o error on "+filename+"check file name and its status"
            
        self.channel = music.play(loops,start)
    def stop(self,fadeout=3):
        fadeout = int(fadeout)
        if fadeout:
            music.fadeout(fadeout*1000)
        else:
            music.stop()
    def pause(self):
        music.pause()
    def uppause(self):
        music.unpause()
    def set_volume(self,volume):
        volume = float(volume) 
        music.set_volume(volume)
    def get_volume(self):
        music.get_volume()
    def balance(self,left,right):
        left,right = float(left),float(right)
        self.channel.set_volume(left, right)

class SE_FX(object):
    def __init__(self):
        self.se_object = None
        
    def play(self,filename,loops=0, maxtime=0, fade_ms=0):
        self.filename = filename
        try:
            self.se_object = pygame.mixer.Sound(filename)
            
        except IOError:
            print "bad filename or i/o error on "+filename+"check file name and its status"
        else:
            self.channel = self.se_object.play(loops, maxtime, fade_ms)
        
    def stop(self,fadeout=0):
        fadeout = int(fadeout)
        if fadeout != 0:
            self.se_boject.fadeout(fadeout)
        else:
            self.se_object.stop() 
    def set_volume(self,volume):
        volume=float(volume)
        self.se_object.set_volume(volume)
        #volume从0到1，浮点数      
    def balance(self,left,right):
        left,right = float(left),float(right)
        self.channel.set_volume(left, right)
            
            
            
            
            
            